package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdQualityCostDao;
import com.ccp.dev.qbdms.dao.WQbdXhdhDao;
import com.ccp.dev.qbdms.model.WQbdQualityCost;
import com.ccp.dev.qbdms.model.WQbdXhdh;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 质量成本 信息操作服务类
 *
 * @author hw
 * @date 2020-07-13 10:17:56
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdQualityCostService extends BaseService<WQbdQualityCost> {

    @Resource
    private WQbdQualityCostDao dao;
    @Resource
    private WQbdXhdhDao wQbdXhdhDao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdQualityCost> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdQualityCost data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    public List<WQbdQualityCost> reportList(String id, String tableName){
        return this.dao.reportList(id,tableName);
    }

    public List<WQbdQualityCost> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdQualityCost> list) {
        List<WQbdXhdh> xhdhList = wQbdXhdhDao.getAll();
        list.forEach(a->{
            a.setId(ProjectUUIDUtils.getUUIDFor32());
            ZhiLiangConst.addCommonFields(a);
            a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            a.setCompanyName(ContextUtil.getCurrentOrg().getOrgName());
            for(WQbdXhdh xhdh : xhdhList){
                if (xhdh.getId().equals(a.getModelCode())) {
                    a.setModelName(xhdh.getfModelname());
                    break;
                }
            }
            BigDecimal count=new BigDecimal(0);
            //预防成本
            a.setPreventionCost(count
                    .add(getValue(a.getQualityOperatingExpenses()))
                    .add(getValue(a.getQualityEdducationTrainFee()))
                    .add(getValue(a.getQualityReviewFee()))
                    .add(getValue(a.getQualityIncentiveFee()))
                    .add(getValue(a.getQualityInprovementMeasure()))
                    .add( getValue(a.getQualityManagementPersonnel()))
            );
            //鉴定成本
             a.setAppraisalCosts( count
                     .add (getValue(a.getInspectionTestOfficeExpense()))
                     .add( getValue(a.getEquipmentDepreciationCost()))
                     .add( getValue(a.getInspectionTestFee()))
                     .add( getValue(a.getSalaryAppraisers()))
                     .add( getValue(a.getEquipmentMaintenance()))
                     .add( getValue(a.getAppraisalCostOther()))
             );
            //内部故障成本
            a.setInternalFailureCost(  count
                    .add (getValue(a.getScrapLossExpenses()))
                    .add( getValue(a.getChangeDesignLossFee()))
                    .add( getValue(a.getSparePartsCost()))
                    .add( getValue(a.getAccidentAnalysisLoss()))
                    .add( getValue(a.getReworkRepairLoss()))
                    .add( getValue(a.getProductDegradationLossFee()))
                    .add( getValue(a.getStopLoss()))
                    .add( getValue(a.getTestLoss()))
                    .add( getValue(a.getInternalFailureCostOther()))
            );
            //外部故障成本
            a.setExternalFailureCost(  count
                    .add( getValue(a.getClaimForLoss()))
                    .add( getValue(a.getProductPriceReductionLoss()))
                    .add( getValue(a.getLossOnReturns()))
                    .add( getValue(a.getLegalFees()))
                    .add( getValue(a.getMaintenanceFee()))
                    .add( getValue(a.getExternalFailureCostOther()))
            );
            //外部故障成本（A）
            a.setExternalFailureCostA( count
                    .add( getValue(a.getClaimForLossA()))
                    .add( getValue(a.getProductPriceReductionLossA()))
                    .add( getValue(a.getLossOnReturnsA()))
                    .add( getValue(a.getLegalFeesA()))
                    .add( getValue(a.getMaintenanceFeeA()))
                    .add( getValue(a.getExternalFailureCostOtherA()))
            );
            //外部故障成本（B）
            a.setExternalFailureCostB( count
                    .add( getValue(a.getClaimForLossB()))
                    .add( getValue(a.getProductPriceReductionLossB()))
                    .add( getValue(a.getLossOnReturnsB()))
                    .add( getValue(a.getLegalFeesB()))
                    .add( getValue(a.getMaintenanceFeeB()))
                    .add( getValue(a.getExternalFailureCostOtherB()))
            );
            //外部故障成本（C）
            a.setExternalFailureCostC(count
                    .add( getValue(a.getClaimForLossC()))
                    .add( getValue(a.getProductPriceReductionLossC()))
                    .add( getValue(a.getLossOnReturnsC()))
                    .add( getValue(a.getLegalFeesC()))
                    .add( getValue(a.getMaintenanceFeeC()))
                    .add( getValue(a.getExternalFailureCostOtherC()))
            );

        });
        if(!list.isEmpty()){
            dao.addList(list);
        }
    }

    private BigDecimal getValue(BigDecimal value){
        if (value==null){
            return BigDecimal.valueOf(0);
        }
        return value;
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdQualityCost getParentAndChilden(String id) {
        return dao.getById(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdQualityCostList 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdQualityCost> wQbdQualityCostList,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdQualityCostList,extendSysFileList);
        List<WQbdQualityCost> qualityCostList = new ArrayList<>();
        for(WQbdQualityCost wQbdQualityCost : wQbdQualityCostList){
            if(StringUtil.isEmpty(wQbdQualityCost.getFilldeptHospitalId())){
                wQbdQualityCost.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQualityCost.getFilldeptid()));
            }else{
                wQbdQualityCost.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQualityCost.getFilldeptHospitalId()));
            }
            // 直接插主表数据
            qualityCostList.add(wQbdQualityCost);
        }
        return super.addList(qualityCostList);
    }
}
